package com.xtgg.more;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;
import com.xtgg.R;
import com.xtgg.history.ShareActivity;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.ToastUtil;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class AboutActivity extends BaseActivity implements OnClickListener {

	private Dialog progressDialog;
	String mTel = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about);

		((TextView) findViewById(R.id.center)).setText("关于我们");
		MobclickAgent.onEvent(this, ((TextView) findViewById(R.id.center))
				.getText().toString());

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.action0).setOnClickListener(this);
		findViewById(R.id.action1).setOnClickListener(this);
		findViewById(R.id.action2).setOnClickListener(this);

		try {
			((TextView) findViewById(R.id.version)).setText(getVersionName());
			((TextView)findViewById(R.id.value0)).setText(getVersionName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		sendHttpRequest();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;

		case R.id.action0:
//			ToastUtil.showToastWithOkPic("已经是最新版本");
//			UmengUpdateAgent.forceUpdate(this);
		{

			progressDialog = new Dialog(this, R.style.dialog);
			progressDialog.setContentView(R.layout.dialog_wait);
			progressDialog.setCancelable(true);
			progressDialog.show();
			
			UmengUpdateAgent.setUpdateAutoPopup(false);
			UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
				@Override
				public void onUpdateReturned(int arg0, UpdateResponse arg1) {
					if(progressDialog != null && progressDialog.isShowing())
					{
						progressDialog.dismiss();
					}
					
			        switch (arg0) {
			        case UpdateStatus.Yes: // has update
			            UmengUpdateAgent.showUpdateDialog(AboutActivity.this, arg1);
			            break;
			        case UpdateStatus.No: // has no update
			        	showToast("没有更新");
						UmengUpdateAgent.setUpdateAutoPopup(true);
			            break;
			        case UpdateStatus.NoneWifi: // none wifi
			        	showToast("没有wifi连接， 只在wifi下更新");
						UmengUpdateAgent.setUpdateAutoPopup(true);
			            break;
			        case UpdateStatus.Timeout: // time out
			        	showToast("超时");
						UmengUpdateAgent.setUpdateAutoPopup(true);
			            break;
			        }
				}
			});
			UmengUpdateAgent.update(this);
		}
			break;

		case R.id.action1:
			Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + mTel));  
            startActivity(intent);  
			break;

		case R.id.action2:
			showQuery();
			break;
		}
	}

	Dialog mDialog = null;
	void showQuery() {
		final OnClickListener click = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				switch (arg0.getId()) {case R.id.action0:
				{
					Intent intent = new Intent(AboutActivity.this,ShareActivity.class);
					intent.putExtra("type", 0);
					intent.putExtra("content", "我正在使用小兔乖乖，你也一起使用吧");
					startActivity(intent);
				}
					mDialog.dismiss();
					break;

				case R.id.action1:
				{
					Intent intent = new Intent(AboutActivity.this,ShareActivity.class);
					intent.putExtra("type", 1);
					intent.putExtra("content", "我正在使用小兔乖乖，你也一起使用吧");
					startActivity(intent);
				}
					mDialog.dismiss();
					break;

				case R.id.action2:
				{
					Intent intent = new Intent(AboutActivity.this,ShareActivity.class);
					intent.putExtra("type", 2);
					intent.putExtra("content", "我正在使用小兔乖乖，你也一起使用吧");
					startActivity(intent);
				}
					mDialog.dismiss();
					break;

				case R.id.action3:
				{
					Intent intent = new Intent(AboutActivity.this,ShareActivity.class);
					intent.putExtra("type", 3);
					intent.putExtra("content", "我正在使用小兔乖乖，你也一起使用吧");
					startActivity(intent);
				}
					mDialog.dismiss();
					break;
				
				case R.id.login:
					mDialog.dismiss();
					break;
				}
			}
		};
		
		
		mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_share);
		mDialog.findViewById(R.id.action0).setOnClickListener(click);
		mDialog.findViewById(R.id.action1).setOnClickListener(click);
		mDialog.findViewById(R.id.action2).setOnClickListener(click);
		mDialog.findViewById(R.id.action3).setOnClickListener(click);
		mDialog.findViewById(R.id.login).setOnClickListener(click);
		mDialog.show();
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject obj = JSON.parseObject(response);
				if (obj.getString("state").equals("1")) {
					mTel = obj.getJSONObject("data").getString("typename");
					((TextView)findViewById(R.id.value1)).setText(mTel);
				} 
				else if (obj.getString("state").equals("2")) 
				{showExitDialog();} 
			}
		};

		RequestParams params = new RequestParams();
		params.put("act", "getTel");
		MgqRestClient.get(Constants.INITTIAL, params, loginHandler);
	}
	
	private String getVersionName() throws Exception {
		PackageManager packageManager = getPackageManager();
		PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),
				0);
		String version = packInfo.versionName;
		return version;
	}
}
